/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package calcmvc;

import java.util.ArrayList;
import java.lang.Math;
import java.math.BigInteger;

/**
 *
 * @author Administrator
 */
public class CalcModel {
    
    private ArrayList<Double> numbersInCalc;
    private ArrayList<String> operationsInCalc;
    private CalcController controller;
    private ArrayList<Double> savedNumbers;
    private ArrayList<String> savedOperations;
    private ArrayList<ArrayList> savedNumbersAndOperations;
    private boolean hasSavedCalculation = false;
    
    public CalcModel(CalcController con)
    {
        controller = con;
        numbersInCalc = new ArrayList<Double>();
        operationsInCalc = new ArrayList<String>();
        
        savedNumbers = new ArrayList<Double>();
        savedOperations = new ArrayList<String>();
        
        savedNumbersAndOperations = new ArrayList<ArrayList>();
        savedNumbersAndOperations.add(savedNumbers);
        savedNumbersAndOperations.add(savedOperations);
    }
    
    public void deleteSavedCalculation()
    {
        savedNumbers.clear();
        savedOperations.clear();
        savedNumbersAndOperations.clear();
        hasSavedCalculation = false;
    }
    
    public void startCalculation()
    {
        numbersInCalc = new ArrayList<Double>();
        operationsInCalc = new ArrayList<String>();
    }
    
    public void setSavedCalculation(ArrayList<ArrayList> al)
    {
        savedNumbersAndOperations = al;
        savedNumbers = al.get(0);
        savedOperations = al.get(1);
        hasSavedCalculation = true;
    }
    
    public ArrayList getSavedCalculation()
    {
        return savedNumbersAndOperations;
    }
    
    public void save()
    {
        
    }
    
    public void load()
    {
        numbersInCalc = savedNumbers;
        operationsInCalc = savedOperations;
    }
    
    
    public double calculateTotal()
    {
        double calculatedNumber = 0.0;
        
        double number1;
        double number2;
           
        for(int i = 0; i < operationsInCalc.size(); i++)
        {
            
            
            if (operationsInCalc.get(i).equals("*"))
            {
                number1 = numbersInCalc.get(i);
                number2 = numbersInCalc.get(i+1);
                
                calculatedNumber = (number1 * number2);
                numbersInCalc.set(i+1, calculatedNumber);
                
                //remove froms lists
                numbersInCalc.remove(i);
                operationsInCalc.remove(i);
                
                i--;
                continue;
            }
            
            else if (operationsInCalc.get(i).equals("/"))
            {
                number1 = numbersInCalc.get(i);
                number2 = numbersInCalc.get(i+1);
                
                calculatedNumber = (number1 / number2);
                numbersInCalc.set(i+1, calculatedNumber);
                
                //remove from lists
                numbersInCalc.remove(i);
                operationsInCalc.remove(i);
                
                //start loop over
                i--;
                continue;
            }
            
            
            
        }
        
        for(int n = 0; n < operationsInCalc.size(); n++)
        {
            number1 = numbersInCalc.get(n);
            number2 = numbersInCalc.get(n+1);
            
            if (operationsInCalc.get(n).equals("+"))
            {
                number1 = numbersInCalc.get(n);
                number2 = numbersInCalc.get(n+1);
                
                calculatedNumber = (number1 + number2);
                
                numbersInCalc.set(n+1, calculatedNumber);
                
                
                numbersInCalc.remove(n);
                operationsInCalc.remove(n);
                
                n--;
                continue;
                }
            
            else if (operationsInCalc.get(n).equals("-"))
            {
                number1 = numbersInCalc.get(n);
                number2 = numbersInCalc.get(n+1);
                
                calculatedNumber = (number1 - number2);
                numbersInCalc.set(n+1, calculatedNumber);
                
                
                numbersInCalc.remove(n);
                operationsInCalc.remove(n);
                
                n--;
                continue;
                
            }
        }
        
        calculatedNumber = 0;
        
        for(int j = 0; j < numbersInCalc.size(); j++)
        {
            calculatedNumber += numbersInCalc.get(j);
        }
              
        //return
        return calculatedNumber; 
        
    }
    
    public boolean hasSavedCalculation()
    {
        return hasSavedCalculation;
    }
    
    public void addNumber(double d)
    {
        numbersInCalc.add(d);
    }
    
    public void addOperation(String s)
    {
        assert(s.length() == 1);
        operationsInCalc.add(s);
    }
    
    public void debugCalcTotal()
    {
        for(int i = 0; i < numbersInCalc.size(); i++)
        {
            //System.out.println("number: " + numbersInCalc.get(i));
        }
        
        for(int i = 0; i < operationsInCalc.size(); i++)
        {
            //System.out.println("operation: " + operationsInCalc.get(i));
        }
    }
    
    public void clear()
    {
        numbersInCalc = new ArrayList<Double>();
        operationsInCalc = new ArrayList<String>();
    }
    
    public ArrayList getNumbersInCalc()
    {
        return numbersInCalc;
    }
    
    public ArrayList getOperationsInCalc()
    {
        return operationsInCalc;
    }

}
